package com.dk.service.impl;

import cn.hutool.core.util.StrUtil;
import com.alibaba.nacos.client.naming.utils.CollectionUtils;
import com.dk.model.Department;
import com.dk.mapper.DepartmentMapper;
import com.dk.service.DepartmentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dk.vo.DepartmentVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author wbh
 * @since 2023-03-22
 */
@Service
public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper, Department> implements DepartmentService {
    @Resource
    private DepartmentMapper departmentMapper;

    @Override
    public List<DepartmentVO> getInfoList(String departmentName) {
        List<Department> departmentList = departmentMapper.selectList(null);
        List<DepartmentVO> list = getInfoListJson(departmentList, 0);
        //如果有条件去查询,没有就直接返回
        if (StringUtils.isNotBlank(departmentName)) {
            list = filterByDepartmentName(list, departmentName);
        }
        return list;
    }

    private List<DepartmentVO> getInfoListJson(List<Department> departmentList, Integer pid) {
        List<DepartmentVO> list = new ArrayList<>();
        for (Department department : departmentList) {
            DepartmentVO vo = null;
            if (department.getPid() == pid) {
                vo = new DepartmentVO();
                BeanUtils.copyProperties(department, vo);
                vo.setDepartmentVOList(getInfoListJson(departmentList, department.getDepartmentId()));
            }
            if (vo != null) {
                List<DepartmentVO> vos = vo.getDepartmentVOList();
                if (vos.size() == 0) {
                    vos.remove(vos);
                }
                list.add(vo);
            }
        }
        return list;
    }

    //迭代器遍历迭代递归模糊查询
    private List<DepartmentVO> filterByDepartmentName(List<DepartmentVO> list, String departmentName) {
        Iterator<DepartmentVO> parent = list.iterator();
        while (parent.hasNext()) {
            //当前节点
            DepartmentVO t = parent.next();
            if (StrUtil.isNotEmpty(departmentName) && !t.getDepartmentName().contains(departmentName)) {
                //当前节点不包含关键字，继续遍历下一级
                // 取出下一级节点
                List<DepartmentVO> children = t.getDepartmentVOList();
                // 递归
                if (!CollectionUtils.isEmpty(children)) {
                    filterByDepartmentName(children, departmentName);
                }
                //下一级节点都被移除了，那么父节点也移除，因为父节点也不包含关键字
                if (CollectionUtils.isEmpty(t.getDepartmentVOList())) {
                    parent.remove();
                }
            } else {
                //当前节点包含关键字，继续递归遍历
                //子节点递归如果不包含关键字则会进入if分支被删除
                List<DepartmentVO> children = t.getDepartmentVOList();
                // 递归
                if (!CollectionUtils.isEmpty(children)) {
                    filterByDepartmentName(children, departmentName);
                }
            }
        }
        return list;
    }
}
